﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Shoozla.ViewModel;
using Shoozla.View;
using System.Threading;
using Shoozla.DataModel;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace Shoozla
{
    /// <summary>
    /// Interaction logic for FolderListView.xaml
    /// </summary>
    public partial class FolderListView : UserControl
    {
 
        public FolderListView()
        {
            InitializeComponent();
            FolderListViewModel folderList = new FolderListViewModel(App.MainView);
            DataContext = folderList;
        }

       
        private void folderLV_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
  
        }


        private void filterTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (AppModel.Songs == null || DataContext == null)
                return;
            
            //display only song thats match the input string
            if (!String.IsNullOrEmpty(filterTB.Text))
            {
                String searchpattern = filterTB.Text.ToLower();

                ((FolderListViewModel)DataContext).DisplayedSongs.Filter = item =>
                {
                    Mp3File m = item as Mp3File;
                    //String selectedFolder = ((FolderListViewModel)DataContext).SelectedFolder.FullName;
                    //if (!m.FileName.FullName.StartsWith(selectedFolder))
                    //    return false; //filter only for selected folder

                    if (m.Artist.ToLower().Contains(searchpattern) || m.Title.ToLower().Contains(searchpattern) || m.AlbumName.ToLower().Contains(searchpattern))
                        return true;
                    else
                        return false;
                };
            }
            else
                ((FolderListViewModel)DataContext).DisplayedSongs.Filter = item => { return true; };
        }

        private void filterTB_GotFocus(object sender, RoutedEventArgs e)
        {
            filterTB.Text = String.Empty;
        }

        GridViewColumnHeader _lastHeaderClicked = null;
        ListSortDirection _lastDirection = ListSortDirection.Ascending;

        void GridViewColumnHeaderClickedHandler(object sender,RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;

            if (headerClicked != null)
            {
                if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
                {
                    if (headerClicked != _lastHeaderClicked)
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (_lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }

                    string header = headerClicked.Column.Header as string;
                    Sort(header, direction);

                    _lastHeaderClicked = headerClicked;
                    _lastDirection = direction;
                }
            }
        }

        private void Sort(string sortBy, ListSortDirection direction)
        {
            try
            {
                if (sortBy == "Cover")
                    return; //do not sort

                //album sort doesn't work

                ICollectionView dataView = CollectionViewSource.GetDefaultView(folderLV.ItemsSource);
                dataView.SortDescriptions.Clear();
                SortDescription sd = new SortDescription(sortBy, direction);
                dataView.SortDescriptions.Add(sd);
                dataView.Refresh();
            }
            catch(Exception sortEx)
            {
                AppModel.LogEntry(sortEx.Message);
            }
        }

        protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            ID3TagEditorView tagEditor = new ID3TagEditorView();
            tagEditor.SelectedSongChanged((Mp3File)folderLV.SelectedItem);
            tagEditor.ShowDialog();
        }

        private void HeaderChkBox_Loaded(object sender, RoutedEventArgs e)
        {
           
            // Do some stuff to initialize it
        }

    }
}
